package com.cloudwise.service.impl;

import com.cloudwise.dao.TestResultDao;
import com.cloudwise.dao.TesterDao;
import com.cloudwise.entity.TestResult;
import com.cloudwise.entity.Tester;
import com.cloudwise.entity.TesterVo;
import com.cloudwise.service.TesterService;
import com.cloudwise.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：wangcf
 * @date ：Created in 2022/1/18 15:32
 * @description：测试者业务实现类
 * @modified By：
 * @version: 1.0
 */
@Service
public class TesterServiceImpl implements TesterService {
    @Resource
    private TesterDao testerDao;

    @Resource
    private TestResultDao testResultDao;

    @Override
    public boolean saveTester(Tester tester) {
        int i = testerDao.saveTester(tester);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public int deleteTester(Integer id) {
        return testerDao.deleteTester(id);
    }


    //事务管理
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteTesterByIds(List<Long> idList) {
        //删除测试者
        int i = testerDao.deleteTesterByIds(idList);
        //删除测试者的测试记录（预留）
        int ids = testResultDao.deleteBatchByTestIds(idList);
        if (i > 0 && ids > 0) {
            return true;
        }
        return false;
    }

    @Override
    public int updateTester(Tester tester) {
        return testerDao.updateTester(tester);
    }

    @Override
    public Tester selectTesterById(Integer id) {
        return testerDao.selectTesterById(id);
    }

    /*@Override
    public List<Tester> selectAllTester(Long page, Long limit, Tester tester) {
        //优化代码，不分页的时候，默认第一页，一页显示10条
        if (page == null) {
            page = Constants.page;
            limit = Constants.limit;
        }
        return testerDao.selectAllTester(page, limit, tester);
    }*/
    @Override
    public List<TesterVo> selectAllTester(Long page, Long limit, Tester tester) {
        //优化代码，不分页的时候，默认第一页，一页显示10条
        if (page == null) {
            page = Constants.page;
            limit = Constants.limit;
        }
        return testerDao.selectAllTester(page, limit, tester);
    }

    @Override
    public Long getCount(Tester tester) {
        return testerDao.getCount(tester);
    }

    /**
     * @param phone
     * @return boolean
     * @create by: wangcf
     * @description:校验手机号是否可以用，true代表可以使用，false不可以使用
     * @create time: 2022/1/18 11:24
     */
    @Override
    public boolean checkPhone(String phone) {

        int i = testerDao.checkPhone(phone);
        if (i > 0) {
            return false;
        }
        return true;
    }

    @Override
    public void deleteNoResult() {
        testerDao.deleteNoResult();
    }
}
